'use strict';

/**
 * Created by twer on 15/3/24.
 */
angular.module('friend')
    .controller('FriendCtrl', [
        '$scope',
        '$rootScope',
        '$routeParams',
        'FriendQueryService',
        'FriendConfirmService',
        'ProfilePhotoService',
        'PhotoHost',
        function ($scope, $rootScope, $routeParams, FriendQueryService, ProfilePhotoService, FriendConfirmService, PhotoHost) {
            $rootScope.visitedUserId = $routeParams.userId;
            var visitedUserId = $routeParams.userId;

            FriendQueryService.query({userId: visitedUserId}, function (response) {
                $scope.friends = response;

                angular.forEach($scope.friends, function (friend) {
                    ProfilePhotoService.query({userId: friend.identity}, function (response) {
                        friend.profilePhotoUri = PhotoHost + 'cdrmimg/' + response.photoId + '?w=60&h=60';
                    });
                });
            });

            $scope.removeFriend = function (friend, index) {
                $scope.friends.splice(index, 1);
                FriendConfirmService.remove({requesterId: visitedUserId, acceptorId: friend.id});
            };
        }])

    .controller('FriendAddRequestCtrl', [
        '$scope',
        'FriendAddModalService',
        'FriendRequestService',
        '$rootScope',
        '$routeParams',
        function ($scope, FriendAddModalService, FriendRequestService, $rootScope, $routeParams) {
            this.close = FriendAddModalService.deactivate;
            var self = this;

            $scope.friendRequest = {message: '我是' + ($rootScope.currentUser.name || '')};

            $scope.send = function () {
                var request = {
                    words: $scope.friendRequest.message,
                    requestId: $rootScope.currentUser.id,
                    friendId: $routeParams.userId
                };

                FriendRequestService.request(request);
                self.close();
            };
        }]);
